package com.xianzhi.controller.content;

import com.xianzhi.constant.PageCodeEnum;
import com.xianzhi.dto.AdDto;
import com.xianzhi.service.AdService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("/ad")
public class AdController {
    @Autowired
    private AdService adService;

    //ModelMap表示在服务器端保存数据
    //之前会使用request.setAttribute(key,value)
    @RequestMapping
    public String init(ModelMap modelMap){
        //表示查询的条件
        AdDto adDto = new AdDto();
        //将查询到的数据保存到request(modelMap)服务器中
        modelMap.addAttribute("list",adService.selectByPage(adDto));
        return "/content/adList";
    }

    @RequestMapping("/search")
    public String search(ModelMap modelMap,AdDto adDto){
        //将查询到的数据保存到request(modelMap)服务器中
        modelMap.addAttribute("list",adService.selectByPage(adDto));
        return "/content/adList";
    }

    @RequestMapping("/addInit")
    public String addInit(){
        return "/content/adAdd";
    }

    @RequestMapping("/add")
    public String add(ModelMap modelMap,AdDto adDto) {
        if(adService.add(adDto)){
            modelMap.addAttribute(PageCodeEnum.KEY,PageCodeEnum.ADD_SUCCESS);
        }else {
            modelMap.addAttribute(PageCodeEnum.KEY,PageCodeEnum.ADD_FAIL);
        }
        return "/content/adAdd";
    }




}
